package controllers

import (
	"encoding/json"
	"fmt"
	"org.yxk/beego-demo/models"
	"org.yxk/beego-demo/utils"

	beego "github.com/beego/beego/v2/server/web"
)

// Operations about Users
type UserController struct {
	beego.Controller
}

// @Title Login
// @Description Logs user into the system333333
// @Param	username		query 	string	true		"The username for login222222222222"
// @Param	password		query 	string	true		"The password for login"
// @Success 200 {string} login success
// @Failure 403 user not exist
// @router /login [get]
func (u *UserController) Login() {
	username := u.GetString("username")
	password := u.GetString("password")
	fmt.Println(username)
	fmt.Println(password)
	u.ServeJSON()
}

// @Title getUserById2233
// @Description 333
// @Success 200 {string}
// @router /getUserById [get]
func (u *UserController) GetUserById() {
	u2 := new(models.UserInfo)
	utils.GetDB().First(&u2)
	re := models.Result{Code: 200, Data: u2}
	u.Data["json"] = re
	u.ServeJSON()
}

// @Title 保存
// @Description 保存接口
// @Success 200 {string}
// @router /save [post]
func (u *UserController) Save() {
	var user models.UserInfo
	json.Unmarshal(u.Ctx.Input.RequestBody, &user)
	fmt.Println(user)
	u.ServeJSON()
}

// @Title 查询列表
// @Description 查询列表
// @Success 200 []
// @router /list [get]
func (u *UserController) List() {
	var userList []models.UserInfo
	utils.GetDB().Find(&userList)
	u.Data["json"] = models.Result{Code: 200, Data: userList}
	u.ServeJSON()
}
